﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Web.Mvc;

namespace ChoViet.Lib.Helpers
{
    public static class MenuHelper
    {
        public static string Menu(this HtmlHelper helper, string className)
        {
            var sb = new StringBuilder();

            // Create opening unordered list tag
            sb.Append(string.Format("<ul class='{0}'>", className));

            // Render each top level node
            var topLevelNodes = SiteMap.RootNode.ChildNodes;
            foreach (SiteMapNode node in topLevelNodes)
            {
                sb.Append(MenuItem(helper, node));
            }

            // Close unordered list tag
            sb.Append("</ul>");
            return sb.ToString();
        }
        private static string MenuItem(this HtmlHelper helper, SiteMapNode parent)
        {
            StringBuilder sb = new StringBuilder();
            if (SiteMap.CurrentNode == parent)
                sb.AppendLine("<li class='current'>");
            else
                sb.AppendLine("<li>");

            sb.AppendFormat("<a href='{0}'>{1}</a>", parent.Url, helper.Encode(parent.Title));
            if (parent.ChildNodes.Count > 0)
            {
                sb.AppendLine("<ul>");
                foreach (SiteMapNode node in parent.ChildNodes)
                {
                    sb.AppendLine(MenuItem(helper, node));
                }
                sb.AppendLine("</ul>");
            }
            sb.AppendLine("</li>");
            return sb.ToString();
        }
    }
}